package com.yao.mall.controller;

import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.IdRequest;
import com.yao.mall.common.Result;
import com.yao.mall.config.BusinessException;
import com.yao.mall.model.dto.AddressRequest;
import com.yao.mall.model.dto.UserRequest;
import com.yao.mall.model.vo.AddressVO;
import com.yao.mall.model.vo.UserVO;
import com.yao.mall.service.AddressService;
import com.yao.mall.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 地址管理控制层
 * @date 2024/3/4 13:54
 */
@RestController
@RequestMapping("/address")
@Slf4j
@Tag(name = "地址信息相关接口")
public class AddressController {

    @Resource
    private AddressService addressService;

    @GetMapping("/get")
    @Operation(summary = "地址查看")
    public Result<List<AddressVO>> getAddress() {
        List<AddressVO> addressVO = addressService.getAddress();
        return Result.success(addressVO);
    }

    @PostMapping("/add")
    @Operation(summary = "添加地址")
    public Result<Boolean> addAddress(@RequestBody AddressRequest addressRequest) {
        Boolean aBoolean = addressService.addAddress(addressRequest);
        return Result.success(aBoolean);
    }

    @PutMapping("/update")
    @Operation(summary = "修改地址")
    public Result<AddressVO> updateAddress(@RequestBody AddressRequest addressRequest) {
        AddressVO addressVO = addressService.updateAddress(addressRequest);
        return Result.success(addressVO);
    }

    @PutMapping("/changeDefault")
    @Operation(summary = "修改默认地址")
    public Result<Boolean> changeDefaultAddress(@RequestBody IdRequest idRequest) {
        Boolean aBoolean = addressService.changeDefaultAddress(idRequest.getId());
        return Result.success(aBoolean);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除地址")
    public Result<Boolean> deleteAddress(@RequestBody IdRequest idRequest) {
        Boolean aBoolean = addressService.deleteAddress(idRequest.getId());
        return Result.success(aBoolean);
    }

}
